home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FM Towns: Free Software Collection 6
/
FM Towns Free Software Collection 6.iso
/
t_os
/
w_t_
/
src
/
wt.c
< prev
Wrap
Text File
|
1993-07-08
|
5KB
|
271 lines
/*
T-MENU ver2.1 level20 用
壁紙張り替えプログラム
[ W.T : ヴェー・テー ]
( Wachseln die Tapete )
Version 3.0 / Level 13
JC2653 . 02 .25
Programmed by 紫羚
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <time.h>
#include <dos.h>
#include <ctype.h>
#include "myfunc.h"
struct List
{
char name[NSIZE] ;
struct List *pointer ;
} *root,*current;
int counter = 0 ;
char tmenu[SIZE] = "" ;
char Dfile[SIZE] ;
/* メイン関数 ( 前処理 ) */
int main(int c,char *v[])
{
int ret = 1 ;
char lzh_file[SIZE] ;
current = root ;
tmpnam(Dfile) ; // テンポラリファイル名の設定
if (c == 2)
{
strcpy(lzh_file,v[1]) ;
sra2yen(lzh_file) ; // / を\に変換する
ret = move(lzh_file) ;
}
else
{
fprintf(stderr,
"W.T. の使い方 : run386 wt lha の圧縮ファイル\n") ;
}
return (ret) ;
}
/* スラッシュを円記号に変換する */
void sra2yen(char *p)
{
char *q = p ;
while (*q != '\0')
{
if (*q == '/')
{
*q = '\\' ;
}
q++ ;
}
}
/* コピーの実行 */
int move(char *lzh)
{
int ret = -1 ;
char fname[NSIZE] = "",*tp,*p ;
struct _stat buf ;
if ((tp = getenv("TMENU")) != NULL)
{
p = strrchr(tp,'\\') ;
if ((p != NULL) && (*(p+1) == '\0'))
{
*p = '\0' ;
}
strcpy(tmenu,tp) ;
strcpy(fname,tmenu) ;
}
strcat(tmenu,"\\tmenu.tif") ;
strcat(fname,"\\") ;
select(lzh,fname) ; // 壁紙ファイルの選択・解凍
if (_stat(fname,&buf) == 0)
{
remove(tmenu) ; // tmenu.tif を消去する。
if (rename(fname,tmenu) != 0)
{
perror("Cannot Move a File ( to Tmenu.Tif ).") ;
}
else
{
fprintf(stderr,"\n壁紙の張り替えが終了しました。\n"
"\t%s ==> %s\n",fname,tmenu) ;
ret = 0 ;
}
}
return(ret) ;
}
/* 壁紙ファイルの選択 */
void select(char *lzh,char *dirpath)
{
char *area,*p,fname[SIZE] ;
Make_lzhList(lzh) ; // .lzh の中のエントリーをファイル化
if ((area = Load_file()) != NULL) // メモリの中にエントリーをロード
{
if (Read_Entry(area) != 0) // .tif ファイルを抽出し、リスト化
{
fprintf(stderr,".lzh ファイルの中に .tif がありません。") ;
}
else
{
p = Select_file(tmrnd() % counter) ;
strcpy(fname,dirpath) ;
strcat(fname," ") ;
strcat(fname,p) ;
strcat(dirpath,p) ;
Melt_file(lzh,fname) ; // 壁紙ファイルを解凍
}
}
}
/* 壁紙ファイルの解凍
入力 = lzh : 圧縮ファイルネーム
tif : 壁紙にするファイルネーム
*/
void Melt_file(char *lzh,char *tif)
{
char Command[SIZE] = "lha x " ;
strcat(Command,lzh) ;
strcat(Command," ") ;
strcat(Command,tif) ;
system(Command) ;
}
/*
壁紙にするファイルをランダムに選択
( リストの n 番目のエントリーを壁紙にする )
*/
char *Select_file(int n)
{
int i ;
struct List *target = root->pointer ;
for (i = 0 ; i < n ; i++)
{
target = target->pointer ;
}
return (target->name) ;
}
/* .lzh ファイルをリダイレクトでファイルに落とす */
void Make_lzhList(char *lzh)
{
char Command[SIZE] = "lha l " ;
strcat(Command,lzh) ;
strcat(Command," > ") ; /* lha l .lzh > tempfile */
strcat(Command,Dfile) ;
system(Command) ;
}
/* リダイレクトしたファイルをメモリーに読み込む
読み込んだ領域へのポインタを返す
*/
char *Load_file(void)
{
char *area = NULL ;
FILE *fp ;
struct _stat Status ;
_stat(Dfile,&Status) ;
if (Status.st_size != 0)
{
area = (char *)malloc(Status.st_size * sizeof(char) +1) ;
if (area == NULL)
{
fprintf(stderr,"メモリーが不足しています。\n") ;
}
else
{
fp = fopen(Dfile,"rb") ;
fread(area,sizeof(char),Status.st_size,fp) ;
strcat(area,"\0") ;
fclose(fp) ;
}
}
else
{
fprintf(stderr,".lzh ファイルを確認してください\n") ;
}
remove(Dfile) ; // テンポラリファイルの抹消
return (area) ;
}
/* area ポインタの指し示す領域から、.tif のついたファイルネームを取り出す */
int Read_Entry(char *area)
{
int i = 1, ret = 0 ;
char *p = area, *q, Entry[12] ;
while ((q = strstr(p,".TIF")) != NULL) // .TIF が領域に存在すれば
{
while (isspace(*(q-i)) == 0) // ホワイトスペースでなければ
{
i++ ;
}
*q = '\0' ;
strcpy(Entry,q-i+1) ;
strcat(Entry,".TIF") ;
if ((ret = Make_fileList(Entry)) != 0) // リスト化できなければ
{
break ;
}
i = 1 ;
p = q+1 ;
}
free(area) ; // 不必要になった領域を開放
return (ret) ;
}
/* p で与えられたポインタの示す文字列をファイルネームリストに追加する */
int Make_fileList(char *p)
{
int ret = 0 ;
struct List *new ;
new = (struct List *)malloc(sizeof(struct List)) ; // 追加領域の確保
if (new != NULL) // 追加する領域を確保できれば
{
counter++ ;
current->pointer = new ;
current = new ;
current ->pointer = 0 ;
strcpy(current->name,p) ;
}
else
{
fprintf(stderr,"メモリーが不足しています。") ;
ret = -1 ;
}
return (ret) ;
}
/* 時間的にランダムを取る */
int tmrnd(void)
{
int i,tmp,ret ;
time_t t ;
time(&t) ;
tmp = (t % MAXINT) + (t / MAXINT) ; // 範囲の限定 (0~MAXINT-1)
for ( i = 0 ; i < tmp ; i++)
{
rand() ;
}
ret = rand() ;
return(ret) ;
}